package com.huihui.activity.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.network.pojo.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 个人资料设置--修改密码
 */
public class SettingDefinePswActivity extends BaseActivity {

    private EditText new_psw;
    private EditText new_pse_confirm;
    private TextView confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_define_psw);
//        getSupportActionBar().setTitle(R.string.define_psw);
        titleView.setText(R.string.define_psw);
        initView();
        initListener();
    }

    private void initView() {
        new_psw = (EditText) findViewById(R.id.new_psw);
        new_pse_confirm = (EditText) findViewById(R.id.new_psw_confirm);
        confirm = (TextView) findViewById(R.id.confirm);
    }

    private void initListener() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确认修改密码
                if(TextUtils.isEmpty(new_psw.getText().toString())){
                    Toast.makeText(getContext(),"新密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(new_psw.getText().toString().length() <= 5){
                    Toast.makeText(getContext(),"新密码长度不能小于6位",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(new_pse_confirm.getText().toString())){
                    Toast.makeText(getContext(),"确认密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!(new_psw.getText().toString()).equals(new_pse_confirm.getText().toString())){
                    Toast.makeText(getContext(),"两次密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }
                doDefinePsw();
            }
        });
    }

    private void doDefinePsw() {
        UserInfo userInfo = getUserInfo();
        if(userInfo == null){
            return;
        }
        OkHttpRequest.getInstance().resetPassword(HttpContants.resetPassword, userInfo.getUser_id(), "", "", new_psw.getText().toString(), new_pse_confirm.getText().toString(), 1, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject != null && jsonObject.getInt(HttpContants.STATE) == HttpContants.SUCCESS_CODE){
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
